<template>
  <el-card>
    <el-upload
        style="text-align: center; padding-bottom: 30px; border-radius: 50%; "
        class="avatar-uploader"
        :action="serverIp+'/files/upload'"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload">
      <img v-if="form.avatarUrl" :src="form.avatarUrl" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
    <el-form :model="form"  label-width="100px" size="medium " style="margin: 0 auto;  width: 450px">
      <el-form-item label="用户名" >
        <el-input v-model="form.username" autocomplete="off" class="inputWidth" disabled></el-input>
      </el-form-item>
      <el-form-item label="密码" >
        <el-input v-model="form.password" autocomplete="off" show-password class="inputWidth" disabled></el-input>
      </el-form-item>

      <el-form-item label="昵称" >
        <el-input v-model="form.nickname" autocomplete="off" class="inputWidth"></el-input>
      </el-form-item>

      <el-form-item label="邮箱" >
        <el-input v-model="form.email" autocomplete="off" class="inputWidth"></el-input>
      </el-form-item>
      <el-form-item label="电话" >
        <el-input v-model="form.phone" autocomplete="off" class="inputWidth"></el-input>
      </el-form-item>
      <el-form-item label="地址" >
        <el-input v-model="form.address" autocomplete="off" class="inputWidth"></el-input>
      </el-form-item>
      <div style="text-align: center; margin-top: 50px;">
        <el-button type="primary" @click="$router.push('/user')" > 返回主页</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>

    </el-form>
  </el-card>
</template>

<script>
import {serverIp} from "../../../public/config";
import {resetRouter} from "@/router";

export default {
  name: "Person",
  data(){
    return{
      serverIp: serverIp,
      imageUrl: '', //图片地址
      form:localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")):{},
    }
  },
  methods:{
    getUser(from){  //存储新的user到浏览器
      //更新浏览器存储器
      console.log(from)
      localStorage.removeItem("user");
      localStorage.setItem("user",JSON.stringify(from))
    },
    save(){
      this.request.post("/user", this.form).then(res=>{
        if(res.code ==="200"){
          this.getUser(this.form);  //存储新的数据到浏览器
          this.openFullScreen2();
        }else {
          this.$message.error(res.msg)
        }
      })
    },
    //图片上传方法
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.avatarUrl = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    //头像加载框
    openFullScreen2() {
      const loading = this.$loading({
        lock: true,
        text: '加载中请稍等',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      });
      setTimeout(() => {
        // 触发父级更新user的方法
        this.$emit("refreshUser")
        this.$message.success("修改成功")
        loading.close();
      }, 1000);
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.inputWidth{
  width: 350px;
}

</style>